package aom.ju.ss.view.user

import android.content.Intent
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.LayoutOrderAddressBinding
import aom.ju.ss.databinding.ListActivityBinding
import aom.ju.ss.lpInterface.BindViewInterface
import aom.ju.ss.mode.ReceivingAddressData
import aom.ju.ss.mode.ReceivingAddressList
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import com.google.gson.Gson

/**
 * 收货地址列表
 */
class AddressListActivity : BaseActivity() {
    private lateinit var binding: ListActivityBinding
    private lateinit var pAdapter: PAdapter
    private var list: MutableList<ReceivingAddressData> = arrayListOf()

    //data 0 选择 1 编辑/删除
    override fun init() {
        binding = viewBinding as ListActivityBinding
        binding.title.backImg.setImageResource(R.drawable.ic_back_black)
        binding.title.title.text = "收货地址"
        binding.title.r_title.text = "添加地址"
        binding.rootView.setBackgroundResource(R.color.color_f5f5f5)
        binding.title.r_title.setOnClickListener {
            startResult(AddEditDelListActivity::class.java, 199, "0")
        }
        binding.list.mRecycler.layoutManager = LinearLayoutManager(activity)
        pAdapter = PAdapter(
            list, R.layout.layout_order_address
        ) { b, position ->
            val ib = b as LayoutOrderAddressBinding
            val info = list[position]
            ib.info = info

            ib.edit.VG(data == "0")
            if (data == "0") {
                ib.edit.setOnClickListener {
                    startResult(
                        AddEditDelListActivity::class.java,
                        199,
                        Gson().toJson(info)
                    )
                }
            }
            ib.layAddress.setOnClickListener {
                if (data == "0") {
                    val ints = Intent()
                    ints.putExtra("data", Gson().toJson(info))
                    setResult(RESULT_OK, ints)
                    finish()
                } else {
                    startResult(AddEditDelListActivity::class.java, 199, Gson().toJson(info))
                }
            }
        }
        binding.list.mRecycler.adapter = pAdapter
        binding.list.small.setEnableLoadMore(false)
        binding.list.small.setOnRefreshListener {
            getAdd()
        }
        getAdd()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) {
            return
        }
        if (requestCode == 199) {
            getAdd()
        }
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ReceivingAddressList::class.java)
        binding.list.small.Fin()
        mode.data.let {
            if (it != null && it.isNotEmpty()) {
                list.clear()
                list.addAll(it)
                pAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        binding.list.small.Fin()
        e.show
    }

    /**
     * 获取收货地址
     */
    private fun getAdd() {
        OKHttpUtils(activity, false)
            .SetApiUrl(LP_API.address_list)
            .POST(this)


    }

    override val lay: Int = R.layout.list_activity
}